package Test_Web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SingleMovie
 */
public class SingleMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SingleMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
		
		// Get Parameters
		String movieId = request.getParameter("id");
		
		try
		{
			try 
			{
				Class.forName("com.mysql.jdbc.Driver").newInstance();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		// Create Connection
		Connection connect;
		connect = DriverManager.getConnection("jdbc:mysql:///moviedb","testuser","testpass");
		Statement tempStatement = connect.createStatement();
		
		// Create and Execute Query
		String command = "select * from movies where id = " + movieId;
		ResultSet resultSet = tempStatement.executeQuery(command);
		
		resultSet.next();
		String id = resultSet.getString("id");
		String title = resultSet.getString("title");
		String year = resultSet.getString("year");
		String director = resultSet.getString("director");
		String banner_url = resultSet.getString("banner_url");
		String trailer_url = resultSet.getString("trailer_url");
		
		out.print("<!DOCTYPE html>");
		out.print("<html>");
		out.print("<body>");
		out.print("");
		out.print("<h1>" + title + "</h1>");
		out.print("");
		out.print("<p>Movie ID = "+ id + "</p>");
		out.print("");
		out.print("<p>Year Released = "+ year + "</p>");
		out.print("");
		out.print("<p>Director = "+ director + "</p>");
		out.print("");
		out.print("<p> Poster = </p><img src = '"+ banner_url +"'>");
		out.print("");
		out.println("<p><a href = '"+ trailer_url +"'>" +
				"Trailer Link" +"</a></p>");
		out.print("");
		
		
		// Display genres
		out.println("<p> Genres = ");
		String genreQuery = "select genres.name, genres.id from movies, genres, genres_in_movies where movies.id = movie_id AND genres.id = genre_id AND movies.id = " +
             id + " GROUP BY genres.id, genres.name";
		Statement tempStatement2 = connect.createStatement();
		ResultSet genreResultSet = tempStatement2.executeQuery(genreQuery);
		
		while(genreResultSet.next())
		{
			String genreId = genreResultSet.getString("id");
			String genreName = genreResultSet.getString("name");
			out.println("<a href = 'BrowseBySingleGenre?genreId="+ genreId +"'>" +
					genreName +"</a> | ");
		}
		out.println("</p>");
		// end display genres
		
		// Display stars
		out.println("<p> star = ");
		String starQuery = "select stars.first_name, stars.last_name, stars.id from movies, stars, stars_in_movies where movies.id = movie_id AND stars.id = star_id AND movies.id = " +
             id + " GROUP BY stars.id";
		Statement tempStatement3 = connect.createStatement();
		ResultSet starResultSet = tempStatement3.executeQuery(starQuery);
		
		while(starResultSet.next())
		{
			String starId = starResultSet.getString("id");
			String firstName = starResultSet.getString("first_name");
			String lastName = starResultSet.getString("last_name");
			String starName = firstName + " " + lastName;
			out.println("<a href = 'SingleStar?id="+ starId +"'>" +
					starName +"</a> | ");
		}
		out.println("</p>");
		// end display stars
	
		// Checkout and Main Menu Link
		out.println("");
		out.println("");
		out.println("");
		out.println("<a href = 'ShoppingCart?movieToAdd="+ movieId +"'>" +
				"CheckOut" +"</a> | " + "<a href = 'MainPage.jsp"+"'>" +
				"Main Menu" + "</a>");
		
		out.print("</body>");
		out.print("</html>");
		out.print("");
		}
		catch (SQLException ex) {
            while (ex != null) {
                  System.out.println ("SQL Exception:  " + ex.getMessage ());
                  ex = ex.getNextException ();
              }  // end while
          }  // end catch SQLException

		catch(java.lang.Exception ex)
          {
              out.println("<HTML>" +
                          "<HEAD><TITLE>" +
                          "MovieDB: Error" +
                          "</TITLE></HEAD>\n<BODY>" +
                          "<P>SQL error in doGet: " +
                          ex.getMessage() + "</P></BODY></HTML>");
              return;
          }
	}

}
